package com.dingpeikeji.quickdeer.modules.rentproduct.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dingpeikeji.quickdeer.core.common.config.Global;
import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.utils.StringUtils;
import com.dingpeikeji.quickdeer.core.common.web.BaseController;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TCompAgreerate;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.TCompAgreerateService;

/**
 * 协议价维护Controller
 * @author wuhao
 * @version 2016-08-08
 */
@Controller
@RequestMapping(value = "${adminPath}/rentproduct/tCompAgreerate")
public class TCompAgreerateController extends BaseController {

	@Autowired
	private TCompAgreerateService tCompAgreerateService;
	
	@ModelAttribute
	public TCompAgreerate get(@RequestParam(required=false) String agreerateid) {
		TCompAgreerate entity = null;
		if (StringUtils.isNotBlank(agreerateid)){
			entity = tCompAgreerateService.get(agreerateid);
		}
		if (entity == null){
			entity = new TCompAgreerate();
		}
		return entity;
	}
	
	@RequiresPermissions("rentproduct:tCompAgreerate:view")
	@RequestMapping(value = {"list", ""})
	public String list(TCompAgreerate tCompAgreerate, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TCompAgreerate> page = tCompAgreerateService.findPage(new Page<TCompAgreerate>(request, response), tCompAgreerate); 
		model.addAttribute("page", page);
		model.addAttribute("tCompAgreerate", tCompAgreerate);
		return "modules/rentproduct/tCompAgreerateList";
	}

	@RequiresPermissions("rentproduct:tCompAgreerate:view")
	@RequestMapping(value = "form")
	public String form(TCompAgreerate tCompAgreerate, Model model) {
		model.addAttribute("tCompAgreerate", tCompAgreerate);
		return "modules/rentproduct/tCompAgreerateForm";
	}

	@RequiresPermissions("rentproduct:tCompAgreerate:edit")
	@RequestMapping(value = "save")
	public String save(TCompAgreerate tCompAgreerate, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, tCompAgreerate)){
			return form(tCompAgreerate, model);
		}
		tCompAgreerateService.save(tCompAgreerate);
		addMessage(redirectAttributes, "保存协议价维护成功");
		return "redirect:"+Global.getAdminPath()+"/rentproduct/tCompAgreerate/?repage";
	}
	
	@RequiresPermissions("rentproduct:tCompAgreerate:edit")
	@RequestMapping(value = "delete")
	public String delete(TCompAgreerate tCompAgreerate, RedirectAttributes redirectAttributes) {
		tCompAgreerateService.delete(tCompAgreerate);
		addMessage(redirectAttributes, "删除协议价维护成功");
		return "redirect:"+Global.getAdminPath()+"/rentproduct/tCompAgreerate/?repage";
	}
	
	@RequestMapping(value = "validateParnertype")
	@ResponseBody
	public String validateParnertype(String parnertype){
		TCompAgreerate tCompAgreerate = new TCompAgreerate();
		tCompAgreerate.setParnertype(parnertype);
		List<TCompAgreerate> list = tCompAgreerateService.findList(tCompAgreerate);
		if(list==null || list.isEmpty()){
			return "true";
		}
		return "false";
	}

}